<?php

namespace addons\helper\controller\wxapp\auth;

use addons\helper\controller\wxapp\Base;
use app\admin\model\AuthGroup;

class Adminlog extends Base
{
    protected $searchFields = 'id,title';
    protected $config = [
        'model'             => '日志',
        'limit'             => 10,
        'defaultSort'       => 'id',
        'defaultOrder'      => 'desc',
        'tabField'          => '',
        'searchPlaceholder' => '输入ID或标题搜索',
        'multi'             => true,
        'search'            => true,
        'sort'              => true,
        'filter'            => true,
        'field' => [
            'admin_id'   => ['管理员ID', 'text', true,  false, '', true, false, false],
            'username'   => ['管理员名', 'text', false,  true, '=', true, false, false],
            'url'        => ['操作页面', 'text', false,  true, 'LIKE', true, false, false],
            'title'      => ['日志标题', 'text', false,  true, 'LIKE', false, false, false],
            'content'    => ['内容', 'text', false,  false, '', true, false, false],
            'ip'         => ['IP', 'text', true,  true, 'LIKE', true, false, false],
            'useragent'  => ['User-Agent', 'text', false,  false, '', true, false, false],
            'createtime' => ['操作时间', 'datetime', true, true, 'RANGE', true, false, false],
        ],
        'fieldContent' => [
            'groups' => []
        ],
        // [可选]前端表单验证规则
        'rules' => [
            
        ],
        'auth' => [
            'multi' => [
                'del'  => [
                    'name'        => '删除',
                    'color'       => '#e74c3c',
                    'icon'        => 'fa-trash',
                    'url'         => 'auth.adminlog/del'
                ]
            ],
            'more' => [
                'del'  => [
                    'name'  => '删除',
                    'color' => '#e74c3c',
                    'url'   => 'auth.adminlog/del'
                ],
            ],
        ],
    ];

    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AdminLog;
        $this->config['field'] = $this->formatField();
        $this->config['pk'] = $this->model->getPk() ? $this->model->getPk() : 'id';

        $this->childrenAdminIds = $this->getChildrenAdminIds(true);
        $this->childrenGroupIds = $this->getChildrenGroupIds($this->isSuperAdmin() ? true : false);

        $groupName = AuthGroup::where('id', 'in', $this->childrenGroupIds)
                ->column('id,name');

        $this->config['fieldContent']['groups'] = $groupName;
    }

    public function index()
    {
        if ($this->request->isPost())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->where('admin_id', 'in', $this->childrenAdminIds)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->where('admin_id', 'in', $this->childrenAdminIds)
                    ->order($sort, $order)
                    ->limit($offset, $this->config['limit'])
                    ->select();

            $result = array('total' => $total, 'rows' => $list);
            
            $data = [
                'tabList'  => null,
                'dataList' => $result,
                'config'   => $this->config
            ];
            $this->success('ok', $data);
        }
    }

    public function del()
    {
        $ids = $this->request->post('ids/a');
        if ($ids)
        {
            $childrenGroupIds = $this->childrenGroupIds;
            $adminList = $this->model->where('id', 'in', $ids)->where('admin_id', 'in', function($query) use($childrenGroupIds) {
                        $query->name('auth_group_access')->field('uid');
                    })->select();
            if ($adminList)
            {
                $deleteIds = [];
                foreach ($adminList as $k => $v)
                {
                    $deleteIds[] = $v->id;
                }
                if ($deleteIds)
                {
                    $this->model->destroy($deleteIds);
                    $this->success();
                }
            }
        }
        $this->error();
    }

    public function add()
    {
        $this->error();
    }

    public function edit()
    {
        $this->error();
    }

    public function multi()
    {
        $this->error();
    }

}
